using System;
using System.ComponentModel;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using System.Collections.Generic;

namespace RollManager.Library.ActionSteps.BaseClasses
{
    [Serializable]
    public abstract class TeamBuildStep : ActionStep
    {
        private string m_buildName = StepConstants.NAME;
        private string m_teamProject = "FMH-ComBiz";
        private string m_teamUrl = "http://vsts01:8080/";

        [NonSerialized]
        private static Dictionary<string, IBuildServer> s_teamServers = new Dictionary<string, IBuildServer>();

        [DisplayName( "Build Name" )]
        [Description( "The name of the build." )]
        public string BuildName
        {
            get
            {
                return m_buildName;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Build Name cannot be empty." );
                }
                else
                {
                    m_buildName = value.Trim();
                }
            }
        }

        [DisplayName( "Team Project" )]
        [Description( "The Team Project that the build is in." )]
        public string TeamProject
        {
            get
            {
                return m_teamProject;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Team Project cannot be empty." );
                }
                else
                {
                    m_teamProject = value.Trim();
                }
            }
        }

        [DisplayName( "Team Server Url" )]
        [Description( "The URL of the location of the team server to connect to." )]
        public string TeamUrl
        {
            get
            {
                return m_teamUrl;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Team Server Url cannot be empty." );
                }
                else
                {
                    m_teamUrl = value.Trim();
                }
            }
        }

        protected IBuildServer getBuildServer()
        {
            IBuildServer server = null;
            if( s_teamServers.TryGetValue( m_teamUrl, out server ) )
            {
                return server;
            }

            lock( s_teamServers )
            {
                if( s_teamServers.TryGetValue( m_teamUrl, out server ) )
                {
                    return server;
                }

                TeamFoundationServer teamServer = TeamFoundationServerFactory.GetServer( m_teamUrl );
                if( teamServer == null )
                {
                    throw new Exception( string.Format( "Couldn't connect to team server {0}.", m_teamUrl ) );
                }

                server = (IBuildServer) teamServer.GetService( typeof( IBuildServer ) );
                if( server == null )
                {
                    throw new Exception( string.Format( "Couldn't get build service for {0}.", m_teamUrl ) );
                }

                s_teamServers[ m_teamUrl ] = server;
                return server;
            }
        }
    }
}
